
package com.mengmeizi.threads;

import com.mengmeizi.interfaces.JsonInflater;
import com.mengmeizi.net.HttpResponse;
import com.mengmeizi.net.HttpUtils;
import com.mengmeizi.net.Method;
import com.mengmeizi.net.NetWorkTool;
import com.mengmeizi.util.LogOut;

import android.content.Context;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Map;

public class URLDataThread extends Thread {

    /** 正在下载中... */
    public static final int TYPE_LOADING = 1;

    /** 下载完成。 */
    public static final int TYPE_LOADED = 2;

    /** 被打断了。 */
    public static final int TYPE_BREAK = 3;

    private Context context;

    private JsonInflater jsonInflater;

    private String url;

    private int what;

    /**
     * 取到数据前，是否被外界干扰。如果被外界干扰，则不执行JsonInflater的操作。<br/>
     * 取值范围为：TYPE_LOADING/TYPE_LOADED/TYPE_BREAK。
     */
    private int smoothly;

    private NetWorkTool networkTool;

    private Map<String, String> params;

    private Map<String, File> files;

    private Method method;

    private boolean lock = false;

    public URLDataThread(Context context, JsonInflater inflater, String url, Map<String, String> params,
            Map<String, File> files, Method method, int what) {
        this.context = context;
        this.jsonInflater = inflater;
        this.url = url;
        this.params = params;
        this.files = files;
        this.method = method;
        this.what = what;

        smoothly = TYPE_LOADING;
    }

    @Override
    public void run() {
        int type = JsonInflater.TYPE_OK;
        String jsonStr = null;
        networkTool = new NetWorkTool(context);
        networkTool.setMethod(method);
        networkTool.setURL(url);
        networkTool.setParams(params);
        networkTool.setFiles(files);
        HttpURLConnection httpConn = null;
        if (!lock) {
            httpConn = networkTool.httpConnection();
            lock = true;
        }
        if (httpConn == null) {
            lock = false;
            type = JsonInflater.TYPE_NETWORK_ERROR;
        } else {
            try {
                HttpResponse response = networkTool.execute(httpConn);
                jsonStr = HttpUtils.getString(response);
            } catch (IOException e) {
                e.printStackTrace();
                // if (!url.equals("http://images.adxzzx.com:5001/package")) {
                type = JsonInflater.TYPE_SERVER_ERROR;
                // }
            } finally {
                if (httpConn != null) {
                    httpConn.disconnect();
                }
            }
        }
        if (smoothly != TYPE_BREAK) {
            smoothly = TYPE_LOADED;
            jsonInflater.inflateByJson(type, jsonStr, what);
        }
    }

    public void setSmoothly(int type) {
        smoothly = type;
        LogOut.out(this, "resetSmootyly=" + type);
    }

    public int getSmoothly() {
        return smoothly;
    }
}
